[PersistChildren(false), DefaultProperty("DataSource"), ParseChildren(true), DefaultEvent("ItemCommand"), Designer("System.Web.UI.Design.WebControls.RepeaterDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class Repeater : Control, INamingContainer
{
    // Fields
    private DataSourceSelectArguments _arguments;
    private DataSourceView _currentView;
    private bool _currentViewIsFromDataSourceID;
    private bool _currentViewValid;
    private bool _inited;
    private string _itemType;
    private ModelDataSource _modelDataSource;
    private bool _pagePreLoadFired;
    private bool _requiresDataBinding;
    private string _selectMethod;
    private bool _throwOnDataPropertyChange;
    private ITemplate alternatingItemTemplate;
    private object dataSource;
    private static readonly object EventCallingDataMethods = new object();
    private static readonly object EventCreatingModelDataSource = new object();
    private static readonly object EventItemCommand = new object();
    private static readonly object EventItemCreated = new object();
    private static readonly object EventItemDataBound = new object();
    private ITemplate footerTemplate;
    private ITemplate headerTemplate;
    internal const string ItemCountViewStateKey = "_!ItemCount";
    private ArrayList itemsArray;
    private RepeaterItemCollection itemsCollection;
    private ITemplate itemTemplate;
    private ITemplate separatorTemplate;

    // Events
    [WebCategory("Data"), WebSysDescription("DataBoundControl_CallingDataMethods")]
    public event CallingDataMethodsEventHandler CallingDataMethods
    {
        add
        {
            base.Events.AddHandler(EventCallingDataMethods, value);
        }
        remove
        {
            base.Events.RemoveHandler(EventCallingDataMethods, value);
            if (this._modelDataSource != null)
            {
                this._modelDataSource.CallingDataMethods -= value;
            }
        }
    }

    [WebSysDescription("DataBoundControl_OnCreatingModelDataSource"), WebCategory("Data")]
    public event CreatingModelDataSourceEventHandler CreatingModelDataSource
    {
        add
        {
            base.Events.AddHandler(EventCreatingModelDataSource, value);
        }
        remove
        {
            base.Events.RemoveHandler(EventCreatingModelDataSource, value);
        }
    }

    [WebCategory("Action"), WebSysDescription("Repeater_OnItemCommand")]
    public event RepeaterCommandEventHandler ItemCommand
    {
        add
        {
            base.Events.AddHandler(EventItemCommand, value);
        }
        remove
        {
            base.Events.RemoveHandler(EventItemCommand, value);
        }
    }

    [WebSysDescription("DataControls_OnItemCreated"), WebCategory("Behavior")]
    public event RepeaterItemEventHandler ItemCreated
    {
        add
        {
            base.Events.AddHandler(EventItemCreated, value);
        }
        remove
        {
            base.Events.RemoveHandler(EventItemCreated, value);
        }
    }

    [WebCategory("Behavior"), WebSysDescription("DataControls_OnItemDataBound")]
    public event RepeaterItemEventHandler ItemDataBound
    {
        add
        {
            base.Events.AddHandler(EventItemDataBound, value);
        }
        remove
        {
            base.Events.RemoveHandler(EventItemDataBound, value);
        }
    }

    // Methods
    private DataSourceView ConnectToDataSourceView()
    {
        if (!this._currentViewValid || base.DesignMode)
        {
            if ((this._currentView != null) && this._currentViewIsFromDataSourceID)
            {
                this._currentView.DataSourceViewChanged -= new EventHandler(this.OnDataSourceViewChanged);
            }
            IDataSource modelDataSource = null;
            if (!base.DesignMode && this.IsUsingModelBinders)
            {
                if ((this.DataSourceID.Length != 0) || (this.DataSource != null))
                {
                    throw new InvalidOperationException(SR.GetString("DataControl_ItemType_MultipleDataSources", new object[] { this.ID }));
                }
                CreatingModelDataSourceEventArgs e = new CreatingModelDataSourceEventArgs();
                this.OnCreatingModelDataSource(e);
                if (e.ModelDataSource != null)
                {
                    this.ModelDataSource = e.ModelDataSource;
                }
                this.UpdateModelDataSourceProperties(this.ModelDataSource);
                CallingDataMethodsEventHandler handler = base.Events[EventCallingDataMethods] as CallingDataMethodsEventHandler;
                if (handler != null)
                {
                    this.ModelDataSource.CallingDataMethods += handler;
                }
                modelDataSource = this.ModelDataSource;
            }
            else
            {
                string dataSourceID = this.DataSourceID;
                if (dataSourceID.Length != 0)
                {
                    Control control = DataBoundControlHelper.FindControl(this, dataSourceID);
                    if (control == null)
                    {
                        throw new HttpException(SR.GetString("DataControl_DataSourceDoesntExist", new object[] { this.ID, dataSourceID }));
                    }
                    modelDataSource = control as IDataSource;
                    if (modelDataSource == null)
                    {
                        throw new HttpException(SR.GetString("DataControl_DataSourceIDMustBeDataControl", new object[] { this.ID, dataSourceID }));
                    }
                }
            }
            if (modelDataSource == null)
            {
                modelDataSource = new ReadOnlyDataSource(this.DataSource, this.DataMember);
            }
            else if (this.DataSource != null)
            {
                throw new InvalidOperationException(SR.GetString("DataControl_MultipleDataSources", new object[] { this.ID }));
            }
            DataSourceView view = modelDataSource.GetView(this.DataMember);
            if (view == null)
            {
                throw new InvalidOperationException(SR.GetString("DataControl_ViewNotFound", new object[] { this.ID }));
            }
            this._currentViewIsFromDataSourceID = this.IsDataBindingAutomatic;
            this._currentView = view;
            if ((this._currentView != null) && this._currentViewIsFromDataSourceID)
            {
                this._currentView.DataSourceViewChanged += new EventHandler(this.OnDataSourceViewChanged);
            }
            this._currentViewValid = true;
        }
        return this._currentView;
    }

    protected internal override void CreateChildControls()
    {
        this.Controls.Clear();
        if (this.ViewState["_!ItemCount"] != null)
        {
            this.CreateControlHierarchy(false);
        }
        else
        {
            this.itemsArray = new ArrayList();
        }
        base.ClearChildViewState();
    }

    protected virtual void CreateControlHierarchy(bool useDataSource)
    {
        IEnumerable data = null;
        int dataItemCount = -1;
        if (this.itemsArray != null)
        {
            this.itemsArray.Clear();
        }
        else
        {
            this.itemsArray = new ArrayList();
        }
        if (!useDataSource)
        {
            dataItemCount = (int) this.ViewState["_!ItemCount"];
            if (dataItemCount != -1)
            {
                data = new DummyDataSource(dataItemCount);
                this.itemsArray.Capacity = dataItemCount;
            }
        }
        else
        {
            data = this.GetData();
            ICollection is2 = data as ICollection;
            if (is2 != null)
            {
                this.itemsArray.Capacity = is2.Count;
            }
        }
        if (data != null)
        {
            int itemIndex = 0;
            bool flag = this.separatorTemplate != null;
            dataItemCount = 0;
            if (this.headerTemplate != null)
            {
                this.CreateItem(-1, ListItemType.Header, useDataSource, null);
            }
            foreach (object obj2 in data)
            {
                if (flag && (dataItemCount > 0))
                {
                    this.CreateItem(itemIndex - 1, ListItemType.Separator, useDataSource, null);
                }
                ListItemType itemType = ((itemIndex % 2) == 0) ? ListItemType.Item : ListItemType.AlternatingItem;
                RepeaterItem item = this.CreateItem(itemIndex, itemType, useDataSource, obj2);
                this.itemsArray.Add(item);
                dataItemCount++;
                itemIndex++;
            }
            if (this.footerTemplate != null)
            {
                this.CreateItem(-1, ListItemType.Footer, useDataSource, null);
            }
        }
        if (useDataSource)
        {
            this.ViewState["_!ItemCount"] = (data != null) ? dataItemCount : -1;
        }
    }

    protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments()
    {
        return DataSourceSelectArguments.Empty;
    }

    protected virtual RepeaterItem CreateItem(int itemIndex, ListItemType itemType)
    {
        return new RepeaterItem(itemIndex, itemType);
    }

    private RepeaterItem CreateItem(int itemIndex, ListItemType itemType, bool dataBind, object dataItem)
    {
        RepeaterItem item = this.CreateItem(itemIndex, itemType);
        RepeaterItemEventArgs e = new RepeaterItemEventArgs(item);
        this.InitializeItem(item);
        if (dataBind)
        {
            item.DataItem = dataItem;
        }
        this.OnItemCreated(e);
        this.Controls.Add(item);
        if (dataBind)
        {
            item.DataBind();
            this.OnItemDataBound(e);
            item.DataItem = null;
        }
        return item;
    }

    public override void DataBind()
    {
        if ((!this.IsDataBindingAutomatic || !base.DesignMode) || (base.Site != null))
        {
            this.RequiresDataBinding = false;
            this.OnDataBinding(EventArgs.Empty);
        }
    }

    protected void EnsureDataBound()
    {
        try
        {
            this._throwOnDataPropertyChange = true;
            if (this.RequiresDataBinding && this.IsDataBindingAutomatic)
            {
                this.DataBind();
            }
        }
        finally
        {
            this._throwOnDataPropertyChange = false;
        }
    }

    protected virtual IEnumerable GetData()
    {
        DataSourceView view = this.ConnectToDataSourceView();
        if (view != null)
        {
            return view.ExecuteSelect(this.SelectArguments);
        }
        return null;
    }

    protected virtual void InitializeItem(RepeaterItem item)
    {
        ITemplate itemTemplate = null;
        switch (item.ItemType)
        {
            case ListItemType.Header:
                itemTemplate = this.headerTemplate;
                goto Label_005B;

            case ListItemType.Footer:
                itemTemplate = this.footerTemplate;
                goto Label_005B;

            case ListItemType.Item:
                break;

            case ListItemType.AlternatingItem:
                itemTemplate = this.alternatingItemTemplate;
                if (itemTemplate != null)
                {
                    goto Label_005B;
                }
                break;

            case ListItemType.Separator:
                itemTemplate = this.separatorTemplate;
                goto Label_005B;

            default:
                goto Label_005B;
        }
        itemTemplate = this.itemTemplate;
    Label_005B:
        if (itemTemplate != null)
        {
            itemTemplate.InstantiateIn(item);
        }
    }

    protected override void LoadViewState(object savedState)
    {
        if (this.IsUsingModelBinders)
        {
            Pair pair = (Pair) savedState;
            if (savedState == null)
            {
                base.LoadViewState(null);
            }
            else
            {
                base.LoadViewState(pair.First);
                if (pair.Second != null)
                {
                    ((IStateManager) this.ModelDataSource).LoadViewState(pair.Second);
                }
            }
        }
        else
        {
            base.LoadViewState(savedState);
        }
    }

    protected override bool OnBubbleEvent(object sender, EventArgs e)
    {
        bool flag = false;
        if (e is RepeaterCommandEventArgs)
        {
            this.OnItemCommand((RepeaterCommandEventArgs) e);
            flag = true;
        }
        return flag;
    }

    protected virtual void OnCreatingModelDataSource(CreatingModelDataSourceEventArgs e)
    {
        CreatingModelDataSourceEventHandler handler = base.Events[EventCreatingModelDataSource] as CreatingModelDataSourceEventHandler;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        this.Controls.Clear();
        base.ClearChildViewState();
        this.CreateControlHierarchy(true);
        base.ChildControlsCreated = true;
    }

    protected virtual void OnDataPropertyChanged()
    {
        if (this._throwOnDataPropertyChange)
        {
            throw new HttpException(SR.GetString("DataBoundControl_InvalidDataPropertyChange", new object[] { this.ID }));
        }
        if (this._inited)
        {
            this.RequiresDataBinding = true;
        }
        this._currentViewValid = false;
    }

    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    protected virtual void OnDataSourceViewChanged(object sender, EventArgs e)
    {
        this.RequiresDataBinding = true;
    }

    protected internal override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (this.Page != null)
        {
            this.Page.PreLoad += new EventHandler(this.OnPagePreLoad);
            if (!base.IsViewStateEnabled && this.Page.IsPostBack)
            {
                this.RequiresDataBinding = true;
            }
        }
        if (!base.DesignMode && !string.IsNullOrEmpty(this.ItemType))
        {
            DataBoundControlHelper.EnableDynamicData(this, this.ItemType);
        }
    }

    protected virtual void OnItemCommand(RepeaterCommandEventArgs e)
    {
        RepeaterCommandEventHandler handler = (RepeaterCommandEventHandler) base.Events[EventItemCommand];
        if (handler != null)
        {
            handler(this, e);
        }
    }

    protected virtual void OnItemCreated(RepeaterItemEventArgs e)
    {
        RepeaterItemEventHandler handler = (RepeaterItemEventHandler) base.Events[EventItemCreated];
        if (handler != null)
        {
            handler(this, e);
        }
    }

    protected virtual void OnItemDataBound(RepeaterItemEventArgs e)
    {
        RepeaterItemEventHandler handler = (RepeaterItemEventHandler) base.Events[EventItemDataBound];
        if (handler != null)
        {
            handler(this, e);
        }
    }

    protected internal override void OnLoad(EventArgs e)
    {
        this._inited = true;
        this.ConnectToDataSourceView();
        if (((this.Page != null) && !this._pagePreLoadFired) && (this.ViewState["_!ItemCount"] == null))
        {
            if (!this.Page.IsPostBack)
            {
                this.RequiresDataBinding = true;
            }
            else if (base.IsViewStateEnabled)
            {
                this.RequiresDataBinding = true;
            }
        }
        base.OnLoad(e);
    }

    private void OnPagePreLoad(object sender, EventArgs e)
    {
        this._inited = true;
        if (this.Page != null)
        {
            this.Page.PreLoad -= new EventHandler(this.OnPagePreLoad);
            if (!this.Page.IsPostBack)
            {
                this.RequiresDataBinding = true;
            }
            if ((this.Page.IsPostBack && base.IsViewStateEnabled) && (this.ViewState["_!ItemCount"] == null))
            {
                this.RequiresDataBinding = true;
            }
            this._pagePreLoadFired = true;
        }
    }

    protected internal override void OnPreRender(EventArgs e)
    {
        this.EnsureDataBound();
        base.OnPreRender(e);
    }

    protected override object SaveViewState()
    {
        object obj2 = base.SaveViewState();
        if (!this.IsUsingModelBinders)
        {
            return obj2;
        }
        Pair pair = new Pair {
            First = obj2,
            Second = ((IStateManager) this.ModelDataSource).SaveViewState()
        };
        if ((pair.First == null) && (pair.Second == null))
        {
            return null;
        }
        return pair;
    }

    protected override void TrackViewState()
    {
        base.TrackViewState();
        if (this.IsUsingModelBinders)
        {
            ((IStateManager) this.ModelDataSource).TrackViewState();
        }
    }

    private void UpdateModelDataSourceProperties(ModelDataSource modelDataSource)
    {
        if (modelDataSource == null)
        {
            throw new ArgumentNullException("modelDataSource");
        }
        modelDataSource.UpdateProperties(this.ItemType, this.SelectMethod);
    }

    // Properties
    [DefaultValue((string) null), Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(RepeaterItem)), WebSysDescription("Repeater_AlternatingItemTemplate")]
    public virtual ITemplate AlternatingItemTemplate
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return this.alternatingItemTemplate;
        }
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        set
        {
            this.alternatingItemTemplate = value;
        }
    }

    public override ControlCollection Controls
    {
        get
        {
            this.EnsureChildControls();
            return base.Controls;
        }
    }

    [WebSysDescription("Repeater_DataMember"), DefaultValue(""), WebCategory("Data")]
    public virtual string DataMember
    {
        get
        {
            object obj2 = this.ViewState["DataMember"];
            if (obj2 != null)
            {
                return (string) obj2;
            }
            return string.Empty;
        }
        set
        {
            this.ViewState["DataMember"] = value;
            this.OnDataPropertyChanged();
        }
    }

    [DefaultValue((string) null), Bindable(true), WebCategory("Data"), WebSysDescription("BaseDataBoundControl_DataSource"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public virtual object DataSource
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return this.dataSource;
        }
        set
        {
            if (((value != null) && !(value is IListSource)) && !(value is IEnumerable))
            {
                throw new ArgumentException(SR.GetString("Invalid_DataSource_Type", new object[] { this.ID }));
            }
            this.dataSource = value;
            this.OnDataPropertyChanged();
        }
    }

    [DefaultValue(""), WebSysDescription("BaseDataBoundControl_DataSourceID"), IDReferenceProperty(typeof(DataSourceControl)), WebCategory("Data")]
    public virtual string DataSourceID
    {
        get
        {
            object obj2 = this.ViewState["DataSourceID"];
            if (obj2 != null)
            {
                return (string) obj2;
            }
            return string.Empty;
        }
        set
        {
            this.ViewState["DataSourceID"] = value;
            this.OnDataPropertyChanged();
        }
    }

    [Browsable(true)]
    public override bool EnableTheming
    {
        get
        {
            return base.EnableTheming;
        }
        set
        {
            base.EnableTheming = value;
        }
    }

    [WebSysDescription("Repeater_FooterTemplate"), Browsable(false), DefaultValue((string) null), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(RepeaterItem))]
    public virtual ITemplate FooterTemplate
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return this.footerTemplate;
        }
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        set
        {
            this.footerTemplate = value;
        }
    }

    [Browsable(false), TemplateContainer(typeof(RepeaterItem)), WebSysDescription("WebControl_HeaderTemplate"), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue((string) null)]
    public virtual ITemplate HeaderTemplate
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return this.headerTemplate;
        }
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        set
        {
            this.headerTemplate = value;
        }
    }

    protected bool Initialized
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return this._inited;
        }
    }

    protected bool IsBoundUsingDataSourceID
    {
        get
        {
            return (this.DataSourceID.Length > 0);
        }
    }

    protected bool IsDataBindingAutomatic
    {
        get
        {
            if (!this.IsBoundUsingDataSourceID)
            {
                return this.IsUsingModelBinders;
            }
            return true;
        }
    }

    private bool IsUsingModelBinders
    {
        get
        {
            return !string.IsNullOrEmpty(this.SelectMethod);
        }
    }

    [WebSysDescription("Repeater_Items"), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public virtual RepeaterItemCollection Items
    {
        get
        {
            if (this.itemsCollection == null)
            {
                if (this.itemsArray == null)
                {
                    this.EnsureChildControls();
                }
                this.itemsCollection = new RepeaterItemCollection(this.itemsArray);
            }
            return this.itemsCollection;
        }
    }

    [DefaultValue((string) null), WebSysDescription("Repeater_ItemTemplate"), Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(RepeaterItem))]
    public virtual ITemplate ItemTemplate
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return this.itemTemplate;
        }
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        set
        {
            this.itemTemplate = value;
        }
    }

    [DefaultValue(""), WebSysDescription("DataBoundControl_ItemType"), Themeable(false), WebCategory("Data")]
    public virtual string ItemType
    {
        get
        {
            return (this._itemType ?? string.Empty);
        }
        set
        {
            if (!string.Equals(this._itemType, value, StringComparison.OrdinalIgnoreCase))
            {
                this._itemType = value;
                this.OnDataPropertyChanged();
            }
        }
    }

    private ModelDataSource ModelDataSource
    {
        get
        {
            if (this._modelDataSource == null)
            {
                this._modelDataSource = new ModelDataSource(this);
            }
            return this._modelDataSource;
        }
        set
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            this._modelDataSource = value;
        }
    }

    protected bool RequiresDataBinding
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return this._requiresDataBinding;
        }
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        set
        {
            this._requiresDataBinding = value;
        }
    }

    protected DataSourceSelectArguments SelectArguments
    {
        get
        {
            if (this._arguments == null)
            {
                this._arguments = this.CreateDataSourceSelectArguments();
            }
            return this._arguments;
        }
    }

    [WebSysDescription("DataBoundControl_SelectMethod"), DefaultValue(""), Themeable(false), WebCategory("Data")]
    public virtual string SelectMethod
    {
        get
        {
            return (this._selectMethod ?? string.Empty);
        }
        set
        {
            if (!string.Equals(this._selectMethod, value, StringComparison.OrdinalIgnoreCase))
            {
                this._selectMethod = value;
                this.OnDataPropertyChanged();
            }
        }
    }

    [TemplateContainer(typeof(RepeaterItem)), WebSysDescription("Repeater_SeparatorTemplate"), Browsable(false), DefaultValue((string) null), PersistenceMode(PersistenceMode.InnerProperty)]
    public virtual ITemplate SeparatorTemplate
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return this.separatorTemplate;
        }
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        set
        {
            this.separatorTemplate = value;
        }
    }
}
